import cv2
import matplotlib.pyplot as plt
import numpy as np 
import math

def myfilter(img,kernel):
    h=img.shape[0]
    w=img.shape[1]
    img1=np.zeros((h,w),np.uint8)
    for i in range (1,h-1):
        for j in range (1,w-1):
            sum=0
            for k in range(-1,2):
                for I in range(-1,2):
                    sum+=img[i+k,j+I]*kernel[k+1,I+1]
        img1[i,j]=sum
    return img1

def show (img):
    cv2.imshow('img',img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


#img=cv2.imread('LenaNoise.png',cv2.IMREAD_GRAYSCALE)
#img=cv2.imread('image\LenaNoise.png')
img=cv2.imread('./img/2.jpg')
#img=cv2.imread('imagelyu1.png')
img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#show(filtered_img)
kernel=np.array([[0, 0.2, 0],
            [0.2,0,0.2],
            [0,0.2, 0]])
#kernel=np.array([I-1,-1,-1],
#           [-1,9,-1],
#           [-1,-1,-1]])
#kernel =np.array([[1,0, 0],
#                [0, 0,0],
#                [0,0,-1]])
#kernel=np.array([[0.09474166, 0.11831801, 0.09474166],
#                    [0.11831801,0.14776132,0.11831801],
#                    [0.09474166, 0.11831801, 0.09474166]])
filtered_img=myfilter(img,kernel)
#filtered_img=cv2.filter2D(img.astype('float32'),-1,kernel)
#filtered_img=(np.uint8)(filtered_img)
#filtered_img=filtered_img+128
print(filtered_img)
show(filtered_img)